/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.SiteInfoRepository;
import hope.platform.data.domain.SiteInfo;
import hope.platform.data.server.data.header.SiteDistanceService;
import hope.platform.data.server.data.header.SiteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/11 9:21
 */
@Service
public class SiteInfoServiceImg implements SiteInfoService {

    @Autowired
    private SiteInfoRepository siteInfoRepository;

    @Autowired
    private SiteDistanceService siteDistanceService;


    @Override
    public SiteInfo addSiteInfo(SiteInfo siteInfo) throws Exception {
        List<SiteInfo> siteList = siteInfoRepository
                .findBySerialNumberStartingWith(siteInfo.getSerialNumber());
        String strTmp;
        if (siteList.size() > 0) {
            strTmp = siteInfo.getSerialNumber().concat(String.format("%03d", siteList.size() + 1));
        } else {
            strTmp = siteInfo.getSerialNumber().concat("001");
        }
        siteInfo.setSerialNumber(strTmp);
        siteInfo.setGmtCreate(new Date());
        siteInfo.setGmtModified(new Date());
        SiteInfo result = this.siteInfoRepository.save(siteInfo);
        siteDistanceService.addBySiteInfo(result);
        return result;
    }

    @Override
    public List<SiteInfo> getAllSiteInfo() {
        return this.siteInfoRepository.findAll();
    }

    @Override
    public List<SiteInfo> getSiteInfoBySiteType(String siteType) {
        return this.siteInfoRepository.findBySiteType(siteType);
    }

    @Override
    public SiteInfo getSiteInfoBySerialNumber(String serialNumber) {
        return this.siteInfoRepository.findBySerialNumber(serialNumber);
    }

    @Override
    public SiteInfo updateSiteInfo(SiteInfo siteInfo) throws Exception {
        SiteInfo tmp = this.siteInfoRepository.findBySerialNumber(siteInfo.getSerialNumber());
        if (tmp == null) {
            throw new Exception("siteInfo.serialNumber can't find site in the database");
        }
        boolean modifiedDistance = false;
        tmp.setGmtModified(new Date());
        if (null != siteInfo.getSiteName()) {
            tmp.setSiteName(siteInfo.getSiteName());
        }
        if (null != siteInfo.getLongitude()) {
            tmp.setLongitude(siteInfo.getLongitude());
            modifiedDistance = true;
        }
        if (null != siteInfo.getLatitude()) {
            tmp.setLatitude(siteInfo.getLatitude());
            modifiedDistance = true;
        }
        if (null != siteInfo.getAltitude()) {
            tmp.setAltitude(siteInfo.getAltitude());
        }
        if (null != siteInfo.getDescription()) {
            tmp.setDescription(siteInfo.getDescription());
        }
        if (null != siteInfo.getConstructionTime()) {
            tmp.setConstructionTime(siteInfo.getConstructionTime());
        }
        if (null != siteInfo.getSiteType()) {
            tmp.setSiteType(siteInfo.getSiteType());
        }
        if( null !=siteInfo.getSoftwareVersion()){
            tmp.setSoftwareVersion(siteInfo.getSoftwareVersion());
        }
        if( null != siteInfo.getFrequencyOfShip()){
            tmp.setFrequencyOfShip(siteInfo.getFrequencyOfShip());
        }
        if( null != siteInfo.getLastVisitTime()){
            tmp.setLastVisitTime(siteInfo.getLastVisitTime());
        }
        if(null !=siteInfo.getSiteStatus()){
            tmp.setSiteStatus(siteInfo.getSiteStatus());
        }
        if( null!=siteInfo.getInsidePicture()){
            tmp.setInsidePicture(siteInfo.getInsidePicture());
        }
        if(null !=siteInfo.getOutsidePicture()){
            tmp.setOutsidePicture(siteInfo.getOutsidePicture());
        }
        if(null != siteInfo.getVideoInfo()){
            if(null !=siteInfo.getVideoInfo().getIp()){
                tmp.getVideoInfo().setIp(siteInfo.getVideoInfo().getIp());
            }
            if(null !=siteInfo.getVideoInfo().getPort()){
                tmp.getVideoInfo().setPort(siteInfo.getVideoInfo().getPort());
            }
            if(null !=siteInfo.getVideoInfo().getUser()){
                tmp.getVideoInfo().setUser(siteInfo.getVideoInfo().getUser());
            }
            if(null != siteInfo.getVideoInfo().getPassword()){
                tmp.getVideoInfo().setPassword(siteInfo.getVideoInfo().getPassword());
            }
            if(null !=siteInfo.getVideoInfo().getOther()){
                tmp.getVideoInfo().setOther(siteInfo.getVideoInfo().getOther());
            }
        }
        if(null != siteInfo.getPeopleRelation()){
            tmp.setPeopleRelation(siteInfo.getPeopleRelation());
        }
        if(null != siteInfo.getSoftConfig()){
            if(null != siteInfo.getSoftConfig().getAreas()){
                tmp.getSoftConfig().setAreas(siteInfo.getSoftConfig().getAreas());
            }
            if(null != siteInfo.getSoftConfig().getRadarType()){
                tmp.getSoftConfig().setRadarType(siteInfo.getSoftConfig().getRadarType());
            }
            if(null != siteInfo.getSoftConfig().getBeginDegree()){
                tmp.getSoftConfig().setBeginDegree(siteInfo.getSoftConfig().getBeginDegree());
            }
            if(null != siteInfo.getSoftConfig().getEndDegree()){
                tmp.getSoftConfig().setEndDegree(siteInfo.getSoftConfig().getEndDegree());
            }
            if(null != siteInfo.getSoftConfig().getCollectCount()){
                tmp.getSoftConfig().setCollectCount(siteInfo.getSoftConfig().getCollectCount());
            }
            if(null != siteInfo.getSoftConfig().getCollectInterval()){
                tmp.getSoftConfig().setCollectInterval(siteInfo.getSoftConfig().getCollectInterval());
            }
            if(null != siteInfo.getSoftConfig().getAntennaSpeed()){
                tmp.getSoftConfig().setAntennaSpeed(siteInfo.getSoftConfig().getAntennaSpeed());
            }
            if(null != siteInfo.getSoftConfig().getGain()){
                tmp.getSoftConfig().setGain(siteInfo.getSoftConfig().getGain());
            }
            if(null != siteInfo.getSoftConfig().getReplaceFile()){
                tmp.getSoftConfig().setReplaceFile(siteInfo.getSoftConfig().getReplaceFile());
            }
            if(null != siteInfo.getSoftConfig().getKWave()){
                tmp.getSoftConfig().setKWave(siteInfo.getSoftConfig().getKWave());
            }
            if(null != siteInfo.getSoftConfig().getBWave()){
                tmp.getSoftConfig().setBWave(siteInfo.getSoftConfig().getBWave());
            }
            if(null != siteInfo.getSoftConfig().getKPeriod()){
                tmp.getSoftConfig().setKPeriod(siteInfo.getSoftConfig().getKPeriod());
            }
            if(null != siteInfo.getSoftConfig().getBPeriod()){
                tmp.getSoftConfig().setBPeriod(siteInfo.getSoftConfig().getBPeriod());
            }
        }
        tmp.setGmtModified(new Date());
        SiteInfo result = this.siteInfoRepository.save(tmp);
        if (modifiedDistance) {
            siteDistanceService.updateBySiteInfo(result);
        }
        return result;
    }
}
